/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rad.Java.Common.List;

import com.mysql.jdbc.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import rad.Java.Common.Class.DefaultList;
import rad.Java.Common.Class.IList;
import rad.Java.Common.Class.Person;
import rad.Java.Common.DB.GenerateSql;

/**
 *
 * @author Renato ferreira dos santos
 */
public class Persons extends DefaultList implements IList{
    // <editor-fold defaultstate="collapsed" desc="Declaration of variables">
        private Person p;
    // </editor-fold>

    public Persons()
    {
        p = new Person();
        _gsql = new GenerateSql();
        _gsql.setTable("person");
    }

    // <editor-fold defaultstate="collapsed" desc="Declaration of functions">
        public boolean fillList() {
            _gsql.prepare();
            _gsql.setCommandType(GenerateSql.CommandType.CMD_SELECT);
            _gsql.setValue("","*");

            return mountList();
        }

        public boolean fillListByName(String Name) {
            _gsql.prepare();
            _gsql.setCommandType(GenerateSql.CommandType.CMD_SELECT);
            _gsql.setValue("","*");
            _gsql.setConditionalWhere(" person_name LIKE '%"+Name+"%'");

            return mountList();
        }

        public Person get(int index){
            return (Person) _items.get(index);
        }

        private boolean mountList(){
            try {
                ResultSet rs;
                if (!_gsql.execute(false)) {
                    return false;
                }
                clear();
                while (_gsql.next()) {
                    Person person = new Person();
                    person.get(_gsql.getResultSet());
                    _items.add(person);
                }
                return true;
            }
            // </editor-fold>
            catch (SQLException ex) {
                Logger.getLogger(Persons.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }

        }
    // </editor-fold>
}
